package com.zlikun.lq.l_12_2;

public class PairTest {

	public static void main(String[] args) {
		String [] str = {"21","12","45","1","29","23"};
		Pair<String> pair = ArrayAlg.minMax(str);
		System.out.println(pair.getFirst()+","+pair.getSecond());
		System.out.println(ArrayAlg.<String>middle(str));//<String>可以省略

	}
	
	static class ArrayAlg{
		
		public static Pair<String> minMax(String [] str){
			String min = str[0];
			String max = str[0];
			for (String s : str) {
				if(s.compareTo(max) > 0){
					max = s;
				}
				if(s.compareTo(min) < 0){
					min = s;
				}
			}
			
			return new Pair<String>(max, min);
		}
		
		public static <T> T middle(T [] str){
			return str[str.length/2];
		}
		
		/**
		 * 泛型方法，T extends 表示返回值的原始类型是Comparable，用extends而不是implement原因是表达父子关系
		 * 默认返回值原始类型是Object
		 * @param str
		 * @return
		 */
		
		public static <T extends Comparable> T min(T [] str){
			T min = str[0];
			for (T t : str) {
				if(min.compareTo(t) > 0){
					min = t;
				}
			}
			return min;
		}
	}

}
